package cn.edu.xmu.oomall.share.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.share.service.ShareService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.Depart;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;

/**
 * @author Jianxiang Xie
 * @date 2021/12/5
 */

@RestController
@RefreshScope
@RequestMapping(value = "", produces = "application/json;charset=UTF-8")
public class ShareController {

    @Autowired
    ShareService shareService;

    /**
     * 分享者生成分享链接
     *
     * @param onsaleid      onsaleId
     * @param loginUserId   用户id
     * @param loginUserName 用户名
     * @return ShareRetVo
     */
    @Audit
    @PostMapping("/onsales/{id}/shares")
    public Object genShare(
            @PathVariable("id") Long onsaleid,
            @LoginUser Long loginUserId,
            @LoginName String loginUserName) {
        return Common.decorateReturnObject(shareService.genShare(onsaleid, loginUserId, loginUserName));

    }

    /**
     * 买家查询所有分享记录
     *
     * @param productId   货品id
     * @param beginTime   分享创建时间区间的开始时间
     * @param endTime     分享创建时间区间的结束时间
     * @param page        页
     * @param pageSize    页大小
     * @param loginUserId 登录的用户id
     * @return 分享记录
     */
    @Audit
    @GetMapping("/shares")
    public Object getShares(
            @RequestParam(value = "productId", required = false) Long productId,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(value = "beginTime", required = false) ZonedDateTime beginTime,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @LoginUser Long loginUserId) {
        //System.out.println("loginUserId"+loginUserId);
        return Common.decorateReturnObject(shareService.getSharesByConditions(null, loginUserId, productId, beginTime, endTime, page, pageSize));
    }

    /**
     * 查看商品的详细信息（需登录，从分享模式查看商品）
     *
     * @param sid         分享id
     * @param productId   货品id
     * @param loginUserId 登录的用户id
     * @return 货品的详细信息
     */
    @Audit
    @GetMapping("/shares/{sid}/products/{id}")
    public Object getProduct(
            @PathVariable("sid") Long sid,
            @PathVariable("id") Long productId,
            @LoginUser Long loginUserId,
            @LoginName String loginUserName) {
        return Common.decorateReturnObject(shareService.getProductByShareId(sid, productId, loginUserId,loginUserName));
    }

    /**
     * 管理员查询商品分享记录
     *
     * @param did       shopId
     * @param productId 货品id
     * @param page      页
     * @param pageSize  页大小
     * @return 分享记录
     */
    @Audit(departName = "shops")
    @GetMapping("/shops/{did}/products/{id}/shares")
    public Object getSharesForAdmin(
            @PathVariable("did") Long did,
            @PathVariable("id") Long productId,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Depart Long adminDepartId) {
            if (!adminDepartId.equals(did)) {
                return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非本店管理员"));
            }
        //System.out.println("进入getSharesForAdmin");
        return Common.decorateReturnObject(shareService.getSharesByConditions(did, null, productId, null, null, page, pageSize));
    }

    /**
     * 分享者查询所有分享成功记录
     *
     * @param productId   货品id
     * @param beginTime   分享创建时间区间的开始时间
     * @param endTime     分享创建时间区间的结束时间
     * @param page        页
     * @param pageSize    页大小
     * @param loginUserId 登录的用户id
     * @return 分享成功记录
     */
    @Audit
    @GetMapping("/beshared")
    public Object getSuccessfulShares(
            @RequestParam(value = "货品Id", required = false) Long productId,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(required = false) ZonedDateTime beginTime,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(required = false) ZonedDateTime endTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @LoginUser Long loginUserId) {
        return Common.decorateReturnObject(shareService.getSuccessfulSharesByConditions(loginUserId, null, productId, beginTime, endTime, page, pageSize));
    }

    /**
     * 管理员查询所有分享成功记录
     *
     * @param did       shopId
     * @param productId 货品id
     * @param beginTime 分享创建时间区间的开始时间
     * @param endTime   分享创建时间区间的结束时间
     * @param page      页
     * @param pageSize  页大小
     * @return 分享成功记录
     */
    @Audit(departName = "shops")
    @GetMapping("/shops/{did}/products/{id}/beshared")
    public Object getSuccessfulSharesForAdmin(
            @PathVariable("did") Long did,
            @PathVariable("id") Long productId,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(required = false) ZonedDateTime beginTime,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) @RequestParam(required = false) ZonedDateTime endTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @Depart Long adminDepartId) {
        System.out.println("adminDepartId"+adminDepartId);
        if (!adminDepartId.equals(did)) {
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非本店管理员"));
        }
        return Common.decorateReturnObject(shareService.getSuccessfulSharesByConditions(null, did, productId, beginTime, endTime, page, pageSize));
    }


    /**
     * 计算订单明细对应的返点
     *
     * @param orderItemId 订单明细id
     * @return 返点数量与分享者id 组合对象
     * @modifiedBy XJX
     * @date 2021-12-24 21:43:24
     * 因清算不用测试而废弃
     */
    /*
    @GetMapping("/internal/calculate")
    public InternalReturnObject calculate(@RequestParam(value = "orderItemId") Long orderItemId) {
        var ret = shareService.calculate(orderItemId);
        return new InternalReturnObject(ret.getCode().getCode(), ret.getErrmsg(), ret.getData());
    }
    */
}